<?php

use app\assets\MaskedInputAsset;
use app\models\CardAttr;
use app\models\CardAttrValue;
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/**
 * @var $this yii\web\View
 * @var $card app\models\Card
 */

MaskedInputAsset::register($this);

$this->title = '修改卡';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $form = ActiveForm::begin();?>
<?php echo Html::activeHiddenInput($card, 'id');?>
<?php echo $form->field($card, 'iccid')->textInput(['disabled' => true]);?>
<?php echo $form->field($card, 'msisdn')->textInput(['disabled' => true]);?>
<?php echo $form->field($card, 'imsi')->textInput(['disabled' => true]);?>
<?php /** @var CardAttr $attr */
foreach (CardAttr::find()->andWhere(['uid' => Yii::$app->user->id, 'status' => CardAttr::STATUS_OK])->orderBy('sort DESC, id ASC')->each() as $attr) {
    $attrValue = CardAttrValue::findOne(['uid' => Yii::$app->user->id, 'attr_id' => $attr->id, 'card_id' => $card->id]);
    if (empty($attrValue)) {
        $attrValue = new CardAttrValue();
    }?>
    <div class="form-group required">
        <label class="control-label" for="card-attr-<?php echo $attr->id;?>"><?php echo Html::encode($attr->name);?></label>
        <?php switch ($attr->type) {
            case CardAttr::TYPE_PASSWORD:
                echo Html::activePasswordInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
                break;
            case CardAttr::TYPE_TEXT:
            case CardAttr::TYPE_URL:
            echo Html::activeTextInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
                break;
            case CardAttr::TYPE_MULTI_TEXT:
            case CardAttr::TYPE_RICH_TEXT:
                echo Html::activeTextarea($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
                break;
            case CardAttr::TYPE_NUMBER:
                echo Html::activeInput('number', $attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
                break;
            case CardAttr::TYPE_EMAIL:
                echo Html::activeInput('email', $attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
                break;
            case CardAttr::TYPE_RADIO:
                $options = preg_split('/\r|\n|\r\n/', $attr->options, -1, PREG_SPLIT_NO_EMPTY);
                $attrValue->value = preg_split('/\r|\n|\r\n/', $attrValue->value, -1, PREG_SPLIT_NO_EMPTY);
                echo Html::activeRadioList($attrValue, 'value', array_combine($options, $options), ['name' => 'CardAttrValue_' . $attr->id]);
                break;
            case CardAttr::TYPE_CHECKBOX:
                $options = preg_split('/\r|\n|\r\n/', $attr->options, -1, PREG_SPLIT_NO_EMPTY);
                $attrValue->value = preg_split('/\r|\n|\r\n/', $attrValue->value, -1, PREG_SPLIT_NO_EMPTY);
                echo Html::activeCheckboxList($attrValue, 'value', array_combine($options, $options), ['name' => 'CardAttrValue_' . $attr->id]);
                break;
            case CardAttr::TYPE_DATETIME:
                echo Html::activeTextInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control masked', 'data-mask'=>'9999-99-99 99:99:99', 'name' => 'CardAttrValue_' . $attr->id]);
                break;
            case CardAttr::TYPE_DATE:
                echo Html::activeTextInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control masked', 'data-mask'=>'9999-99-99', 'name' => 'CardAttrValue_' . $attr->id]);
                break;
            case CardAttr::TYPE_TIME:
                echo Html::activeTextInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control masked', 'data-mask'=>'99:99:99', 'name' => 'CardAttrValue_' . $attr->id]);
                break;
            case CardAttr::TYPE_FILE:
                echo Html::activeFileInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'name' => 'CardAttrValue_' . $attr->id]);
                break;
            case CardAttr::TYPE_IMAGE:
                echo Html::activeFileInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'name' => 'CardAttrValue_' . $attr->id]);
                break;
            case CardAttr::TYPE_COLOR:
                echo Html::activeTextInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
                break;
            default:
                echo Html::activeTextInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
        }?>
        <div class="help-block"><?php echo Html::encode($attr->desc);?></div>
    </div>
<?php }?>
<div class="form-group">
    <div class="col-lg-offset-1 col-lg-11">
        <button type="button" class="btn btn-default" onclick="window.history.go(-1);"><i class="ace-icon fa fa-arrow-left bigger-110"></i>返回</button>
        <button class="btn btn-primary"><i class="ace-icon fa fa-check bigger-110"></i>保存</button>
        <button type="reset" class="btn btn-warning"><i class="ace-icon fa fa-undo bigger-110"></i>重置</button>
    </div>
</div>
<?php $form->end();?>
